@echo off
setlocal enabledelayedexpansion

echo ====== 开始安装惠企政策爬虫依赖 ======

REM 安装Node.js依赖
echo 正在安装依赖...
call npm install --save cheerio axios node-cron winston

REM 创建数据库表
echo 正在创建数据库表...
node -e "const mysql = require('mysql2/promise'); const dotenv = require('dotenv'); const fs = require('fs'); const path = require('path'); dotenv.config({ path: '.env.local' }); async function setupDatabase() { try { const dbConfig = { host: process.env.DB_HOST || 'rm-bp14wd51i709c3z79ho.mysql.rds.aliyuncs.com', user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || 'Ircnjfdhnvxwz@123', database: process.env.DB_NAME || 'xiaoyoubao', port: Number(process.env.DB_PORT) || 3306 }; console.log('正在连接数据库...'); const connection = await mysql.createConnection(dbConfig); console.log('数据库连接成功'); console.log('正在创建惠企政策表...'); const sql = fs.readFileSync(path.join(__dirname, '../sql/create_policies_table.sql'), 'utf8'); const statements = sql.split(';').filter(stmt => stmt.trim()); for (const statement of statements) { if (statement.trim()) { await connection.execute(statement + ';'); } } console.log('数据库表创建成功'); await connection.end(); console.log('数据库设置完成'); } catch (error) { console.error('设置数据库失败:', error); process.exit(1); } } setupDatabase();"

REM 创建日志目录
echo 正在创建日志目录...
if not exist logs mkdir logs

REM 设置DeepSeek API密钥
echo 设置DeepSeek API密钥...
set "found="
findstr "DEEPSEEK_API_KEY" .env.local > nul 2>&1 && set "found=1"
if not defined found (
    echo DEEPSEEK_API_KEY=sk-dbc2528168b542acbf58df81f0c3db88 >> .env.local
)

echo ====== 惠企政策爬虫安装完成 ======
echo 你可以使用以下命令运行爬虫:
echo   npm run crawl-policies     # 手动执行一次爬虫
echo   npm run schedule-policies  # 启动定时爬虫任务

pause 